package org.demo.controller;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import org.demo.dao.GradeDao;
import org.demo.dao.StrategeDao;
import org.demo.pojo.Grade;
import org.demo.pojo.Stratege;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.HashMap;
import java.util.Map;

@Controller
@RequestMapping("/Str")
public class StrategeController {
    @Autowired
    private StrategeDao strategeDao;
    @Autowired
    private GradeDao gradeDao;

    @RequestMapping("/index")
    public String index(ModelMap modelMap) throws Exception{
        modelMap.put("gradeList",gradeDao.getAll());
        return "index";
    }

    @RequestMapping("/list")
    @ResponseBody
    public Map<String,Object> list(@RequestParam(defaultValue = "1") Integer page, @RequestParam(defaultValue = "10") Integer limit , Stratege stratege,Integer gid) throws Exception{
        Map<String,Object> map = new HashMap<String, Object>();
        Page<Stratege> page1 = PageHelper.startPage(page,limit);
        if(gid!=null && gid !=0){
            Grade g = new Grade();
            g.setId(gid);
            stratege.setGrade(g);
        }
        map.put("count",page1.getTotal());
        map.put("data",strategeDao.getList(stratege));
        map.put("msg",null);
        map.put("code",0);
        return map;
    }

    @RequestMapping("/del")
    @ResponseBody
    public Map<String,Object> del(Integer id) throws Exception{
        Map<String,Object> map = new HashMap<String, Object>();
        strategeDao.del(id);
        return map;
    }

    @RequestMapping("/getOne")
    @ResponseBody
    public Stratege getOne(Integer id) throws Exception{
        return strategeDao.getOne(id);
    }

    @RequestMapping("/form")
    public String form(ModelMap modelMap) throws Exception{
        modelMap.put("gradeList",gradeDao.getAll());
        return "form";
    }
}
